from django.db import models

class KnowledgeBase(models.Model):
    """
    知识库模型
    """
    id = models.AutoField(auto_created=True, primary_key=True) # 知识库id
    name = models.CharField(max_length=255, unique=True) # 知识库名称
    version = models.CharField(max_length=255, unique=True) # 知识库版本
    created_at = models.DateTimeField(auto_now_add=True)
    department_id = models.IntegerField(verbose_name="Department ID")# 知识库对应的部门名称
    train_status = models.IntegerField(verbose_name="train status")  # 训练状态 0未训练，1训练中，2已发布
    permissions = models.IntegerField(verbose_name="permissions")  # 权限等级 0初级，1中级，2高级
    class1 = models.IntegerField(verbose_name="class1")  #知识库一级分类对应的数据id
    class2 = models.IntegerField(verbose_name="class2")  #知识库二级分类对应的数据id
    class3 = models.IntegerField(verbose_name="class3") #知识库三级分类对应的数据id
    file_nums = models.IntegerField(verbose_name="file_nums") # 文件数量
    change_file_nums = models.IntegerField(verbose_name="change_file_nums") #修改的文件数量

    def __str__(self):
        return self.name

class KnowledgeBaseFile(models.Model):
    id=models.AutoField(auto_created=True, primary_key=True)
    file_name = models.CharField(max_length=255, unique=True)  # 文件名
    file_markdown = models.TextField(blank=True, null=True) # 文件内容转markdown格式用于展示
    file_path = models.CharField(max_length=255, unique=True) #文件minio存储路径，模型训练使用
    tags = models.CharField(max_length=255, blank=True)  # 文件标签，字符串类型
    class1 = models.IntegerField(verbose_name="class1") #文件分类单选必选
    author = models.CharField(max_length=255, blank=True) #文件上传人
    is_enabled = models.BooleanField(default=True)  # 文件是否启用
    permissions = models.IntegerField(verbose_name="permissions") #0代表低级，1代表中级，2代表高级
    likes = models.IntegerField(verbose_name="likes") #点赞数
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"File: {self.file}, Tags: {self.tags}, Source: {self.source}"

class KnowledgeFileMap(models.Model):
    id = models.AutoField(primary_key=True)
    KnowledgeBase = models.ForeignKey(KnowledgeBase, on_delete=models.CASCADE, related_name="KnowledgeBaseFile", verbose_name="KnowledgeBase")
    KnowledgeBaseFile = models.ForeignKey(KnowledgeBaseFile, on_delete=models.CASCADE, related_name="KnowledgeBase",
                                   verbose_name="KnowledgeBaseFile")
    created_at = models.DateTimeField(auto_now_add=True)
    train_status = models.IntegerField(verbose_name="train status")  # 文档训练状态 0未训练，1训练中，2已发布

    class Meta:
        verbose_name = "Knowledge File Mapping"
        verbose_name_plural = "Knowledge File Mappings"
        unique_together = (
        'KnowledgeBase', 'KnowledgeBaseFile')

    def __str__(self):
        return f"{self.KnowledgeBase.name} -> {self.KnowledgeBaseFile.file_name}"

class KnowledgeClass1(models.Model):
    id = models.AutoField(auto_created=True, primary_key=True)  # 类型
    name = models.CharField(max_length=128,verbose_name="类型名")

    class Meta:
        db_table = "KnowledgeClass1"

class KnowledgeClass2(models.Model):
    id = models.AutoField(auto_created=True, primary_key=True)  # 类型
    name = models.CharField(max_length=128,verbose_name="类型名")
    # parent为自关联，关联父id
    parent = models.ForeignKey(KnowledgeClass1, verbose_name='关联父',
                               on_delete=models.CASCADE, related_name="KnowledgeClass1")
    class Meta:
        db_table = "KnowledgeClass2"

class KnowledgeClass3(models.Model):
    id = models.AutoField(auto_created=True, primary_key=True)  # 类型
    name = models.CharField(max_length=128,verbose_name="类型名")
    # parent为自关联，关联父id
    parent = models.ForeignKey(KnowledgeClass2, verbose_name='关联父',
                               on_delete=models.CASCADE, related_name="KnowledgeClass2")
    class Meta:
        db_table = "KnowledgeClass3"

class FileClass1(models.Model):
    id = models.AutoField(auto_created=True, primary_key=True)  # 类型
    name = models.CharField(max_length=128,verbose_name="类型名")

    class Meta:
        db_table = "FileClass1"